﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;
using ServerApi.Models;
using TuyennvLibrary.Reflection;
using TuyennvLibrary.Utl;
using ServerApi.Controllers;
using TuyennvLibrary.ServerApi.Models;
using TuyennvLibrary.ServerApi.Controllers;

namespace ServerApi.administrator
{
    public partial class DocumentApi : System.Web.UI.Page
    {
        ApplicationModel appModel = new ApplicationModel();
        Application app;
        List<DescriptionMethod> list;
        public int stt = 1;
        public string title = string.Empty;
        public string info = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                string appid = Request["id"];
                app = appModel.GetByID(appid);
                string platform = (app.platform == 1) ? "Android" : (app.platform == 2) ? "IOS" : (app.platform == 3) ? "Window Phone" : (app.platform == 4) ? "J2ME" : "WAP";
                title = string.Format("Danh sách API ứng dụng {0}", app.name);
                info = string.Format("Thông tin: <strong>platform</strong> = <strong><i>{0}</i></strong>, <strong>appid</strong> = <strong><i>{1}</i></strong>, <strong>privatekey</strong> = <strong><i>{2}</i></strong>", platform, app.appid, app.private_key);
                list = app.getDocumentApi(Server, Global.PageDevApi, Request);
                GrvListDescription.DataSource = list;
                GrvListDescription.DataBind();
            }
            else
            {
                Response.Write("Thiếu id");
            }
            //string document = app.getDocumentApi(Server);
            //string title = string.Format("<strong>Danh sách API ứng dụng {0}</strong><br />", app.name);
            //Response.Write(title + document);
        }

        protected void GrvListDescription_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DescriptionMethod description = (DescriptionMethod)e.Row.DataItem;
            if (description != null)
            {
                if (!description.Inherit)
                {
                    //e.Row.Cells[1].ForeColor = Color.FromArgb(149, 165, 166);
                    Color color = Color.Black;
                    e.Row.Cells[1].ForeColor = color;
                    e.Row.Cells[2].ForeColor = color;
                }
                else
                {
                    Color color = Color.FromArgb(230, 126, 34);
                    e.Row.Cells[1].ForeColor = color;
                    e.Row.Cells[2].ForeColor = color;
                }
            }
        }
        protected void GrvListDescription_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandname = e.CommandName.ToString();
            if (commandname == "GenAndroidCode")
            {
                try
                {
                    int index = int.Parse(e.CommandArgument.ToString());
                    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    DescriptionMethod description = list[index];
                    description.AndroidCode += "<strong><a href=\"/DownloadFile.aspx?filename=BlueseaLibs.jar\">Tải Api Library</a></strong></br>";
                    description.AndroidCode += "<strong><a href=\"/DownloadFile.aspx?filename=CreateApiConfig.rar\">Tải bộ cài đặt tạo file config</a></strong>";
                    description.AndroidCode = description.AndroidCode.ReplaceAll("\n", "</br>");
                    Utilities.Script(this, string.Empty, "<script>$(function () {$(\"#dialog\").html('" + description.AndroidCode + "'); $(\"#dialog\").dialog({width: 800});});</script>");
                }
                catch (Exception ex)
                {
                }
            }
            if (commandname == "GenWAPCode")
            {
                try
                {
                    int index = int.Parse(e.CommandArgument.ToString());
                    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    DescriptionMethod description = list[index];
                    string ConfigContent = "";
                    string UrlApi = Config.domain + "/POST.aspx";
                    ConfigContent += "ApiConfig config = new ApiConfig(\"" + UrlApi + "\", \"" + app.appid + "\", \"" + app.private_key + "\");\n";
                    string SampleCode = "<strong>Sample</strong>\n";
                    SampleCode += "public class ApiController{\n";
                    SampleCode += ConfigContent;
                    SampleCode += description.WAPCode;
                    SampleCode += "}\n";
                    SampleCode += "<strong><a href=\"/DownloadFile.aspx?filename=BlueseaLib.dll\">Tải Api Library</a></strong>";
                    description.WAPCode = SampleCode;
                    description.WAPCode = description.WAPCode.ReplaceAll("\n", "</br>");

                    Utilities.Script(this, string.Empty, "<script>$(function () {$(\"#dialog\").html('" + description.WAPCode + "'); $(\"#dialog\").dialog({width: 800});});</script>");
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}